package com.qdu.hyy.PopularityCount;

// ...
import com.qdu.hyy.MovieWritable;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class PopularityCountMapper extends Mapper<Object, Text, Text, MovieCountRevenueWritable> {

//    Double revenue ;
    // ...
    @Override
    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

        String[] fields = value.toString().split(","); // 使用逗号作为分隔符

        if (fields.length >= 22) { // 确保有足够的字段

            Text director = new Text(fields[21]); // 假设MovieWritable有getDirector方法

            String popularity = (fields[8]);

            if (popularity != null && !popularity.trim().isEmpty()) {
                try {
                    // 尝试将字符串转换为double
                    Double revenue = Double.parseDouble(popularity.trim());

                    MovieCountRevenueWritable R = new MovieCountRevenueWritable(1, revenue);

                    context.write(director, R);

                    // 转换成功，进行后续处理
                } catch (NumberFormatException e) {
                    // 转换失败，处理异常，例如记录日志或跳过当前记录
                    System.err.println("无法将字符串转换为double: " + popularity);
                }
            } else {
                // 字符串为空，处理空值情况，例如记录日志或跳过当前记录
                System.err.println("voteAverageStr为空");
            }


        }
    }
}